//  axios基础配置
import axios from 'axios'

let config = {
    baseURL: process.env.VUE_APP_BASE_API,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
    },
    // 设置超时时间
    timeout: 10000,
    // 携带凭证 
    withCredentials: false,
    // 返回数据类型
    responseType: 'json'
}

const instance = axios.create(config)//axios实例化

// 以下只是事例，待完善

/* 请求拦截器-----发送请求前发生----------------------- */
instance.interceptors.request.use(config => {
    // console.log('发请求前', config)
    if (localStorage.getItem('Authorization')) {
        const token = localStorage.getItem('Authorization');
        token && (config.headers.Authorization = token);
    }
    return config
}, error => {
    console.log(error)
}
)

/* 响应拦截器-----响应后发生-------------------------- */
instance.interceptors.response.use(response => {
    switch (response.status) {
        case 200:
            // console.log('请求成功');
            break;
        case 404:
            // console.log('找不到页面')
            break;
    }
    return response
}, error => {
    console.log(error)
}
)

export default instance
